<?php
// +----------------------------------------------------------------------
// | HkCms 单页模型
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2021 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: HkCms team <admin@hkcms.cn>
// +----------------------------------------------------------------------

declare (strict_types=1);

namespace app\admin\model\cms;

use think\Model;

class Single extends Model
{
    public static $tablename;

    /**
     * 无需表前缀, 定义表名称
     * @param $name
     * @return $this
     */
    public function setTable($name)
    {
        $this->name = $name;
        self::$tablename = $name;
        return $this;
    }

    /**
     * 格式化url
     * @param $value
     * @param $data
     * @return string
     */
    public function getUrlAttr($value, $data)
    {
        if (empty($value)) {
            $info = Category::where(['id'=>$data['category_id']])->find();
            if ($info) {
                $mode = site('url_mode');
                if ($mode==1) {
                    $value = (string) url('/index/lists', ['list'=>$info['name']],'html');
                } else if ($mode==2) { // /目录/
                    if ($info['parent_id']) {
                        $name = Category::where(['id'=>$info['parent_id']])->value('name');
                        return (string) url('/index/lists', ['list'=>$info['name'],'catdir'=>$name],'').'/';
                    }
                    return (string) url('/index/lists', ['list'=>$info['name']],'').'/';
                } else if ($mode==3) { // /目录/
                    $value = (string) url('/index/lists', ['list'=>$info['name']],'').'/';
                } else if ($mode==4) { // /模型/栏目.html
                    $tb = \app\admin\model\cms\Model::where(['id'=>$data['model_id']])->value('tablename');
                    $tb = strtolower($tb);
                    return (string) url('/index/lists', ['list'=>$info['name'],'m'=>$tb],'').'/';
                } else if ($mode==5) { // /目录.html
                    $value = (string) url('/index/lists', ['list'=>$info['name']],'html');
                }
            }
        }
        return $value;
    }

    /**
     * 固定方法，用于栏目删除后的处理
     * @param $modelInfo
     * @param $categoryId
     * @param bool $force
     * @return bool
     */
    public function handleDel($modelInfo, $categoryId, $force=false)
    {
        return true;
    }

    /**
     * 固定方法。用于内容标签，不需要直接return [];
     * @param $tag
     * @param $page \think\Paginator|mixed paginate 分页对象
     * @return array|mixed|object|\think\App
     */
    public function handleTag($tag, &$page)
    {
        // 缓存设置
        $cacheTime = !empty($tag['cache']) && is_numeric($tag['cache']) ? intval($tag['cache']) : 3600;
        $cacheID = to_guid_string($tag);
        if (!env('APP_DEBUG') && $cacheData = cache($cacheID)) {
            return $cacheData;
        }

        if (empty($tag['catid']) && !is_numeric($tag['catid'])) {
            return [];
        }

        // 获取栏目信息
        $cateInfo = Category::where(['id'=>$tag['catid'],'status'=>'normal'])->find();
        if (empty($cateInfo['model_id'])) {
            return [];
        }
        $modelInfo = \app\admin\model\cms\Model::where(['status'=>'normal','id'=>$cateInfo['model_id']])->find();
        if (empty($modelInfo)) {
            return [];
        }

        $array = $this->setTable($modelInfo->tablename)->where('category_id','=',$tag['catid'])->select()->append(['url'])->toArray();

        // 结果进行缓存
        if (!env('APP_DEBUG')) {
            cache($cacheID, $array, $cacheTime);
        }
        return $array;
    }

    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}